package br.brpv.silvestrin.vendas.listeners;

import br.brpv.silvestrin.vendas.domain.Company;
import br.brpv.silvestrin.vendas.domain.User;
import br.brpv.silvestrin.vendas.mainactivity.CompanyFragment;
import br.brpv.silvestrin.vendas.mainactivity.MainActivity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;


public class TabListener <T extends CompanyFragment> implements ActionBar.TabListener {
    private T mFragment;
    private final MainActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private int indexSelectedCompany = 0;
    private User user;

    /** Constructor used each time a new tab is created.
     * @param activity  The host Activity, used to instantiate the fragment
     * @param tag  The identifier tag for the fragment
     * @param clz  The fragment's Class, used to instantiate the fragment
     */
    @SuppressWarnings("unchecked")
	public TabListener(MainActivity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        mFragment = (T) mActivity.getFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
            ft.detach(mFragment);
            ft.commit();
        }
    }

    @SuppressWarnings("unchecked")
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = (T) Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
            mFragment.setUser(user);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }

        mActivity.setCurrentFragment(mFragment);
        
        if(mActivity.getMenuChangeCompany() != null){
            Company selected = (Company) mActivity.getCompany(indexSelectedCompany);
            if(selected != null) {
            	mActivity.getMenuChangeCompany().setTitle(selected.toString());
                int cod = new Integer(selected.getId());

                mFragment.setSelectedCompany(cod);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    public T getmFragment() {
        return mFragment;
    }

    public int getIndexSelectedCompany() {
        return indexSelectedCompany;
    }

    public void setIndexSelectedCompany(int indexSelectedCompany) {
        this.indexSelectedCompany = indexSelectedCompany;
    }
    
    public void setUser(User user) {
    	this.user = user;
    }
}